package com.ant.storage.service.impl;

import com.ant.storage.pojo.CapitalAccount;
import com.ant.storage.mapper.CapitalAccountMapper;
import com.ant.storage.service.ICapitalAccountService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author melon
 * @since 2023-11-17
 */
@Service
public class CapitalAccountServiceImpl extends ServiceImpl<CapitalAccountMapper, CapitalAccount> implements ICapitalAccountService {

    @Override
    public List<CapitalAccount> costTypeList() {
        List<CapitalAccount> list = this.baseMapper.selectList(new QueryWrapper<CapitalAccount>()
                .eq("status",1).orderByAsc("account_sort"));
        //获取根节点
        List<CapitalAccount> Departments = list.stream().filter(e -> e.getParentId() == 0)
                .map(e -> {
                    e.setChildren(getTreeChildren(e, list));
                    return e;
                }).collect(Collectors.toList());
        return Departments;
    }

    @Override
    public List<CapitalAccount> costTypeLists() {
        List<CapitalAccount> list = this.baseMapper.selectList(new QueryWrapper<CapitalAccount>().orderByDesc("id"));
        //        获取根节点
        List<CapitalAccount> Departments = list.stream().filter(e -> e.getParentId() == 0)
                .map(e -> {
                    e.setChildren(getTreeChildren(e, list));
                    return e;
                }).collect(Collectors.toList());
        return Departments;
    }

    //    List<Department> list = departmentService.list();
    //    //        获取根节点
    //    List<Department> Departments = list.stream().filter(e -> e.getParentId()==1)
    //            .map(e -> {
    //                e.setChildren(getTreeChildren(e, list));
    //                return e;
    //            }).collect(Collectors.toList());
    //        return ResultVo.success("获取成功",Departments);
    //}

    private List<CapitalAccount> getTreeChildren(CapitalAccount e, List<CapitalAccount> list) {
        List<CapitalAccount> children = list.stream()
                .filter(m -> Objects.equals(e.getId(), m.getParentId())).map(m -> {
                    m.setChildren(getTreeChildren(m, list));
                    return m;
                }).collect(Collectors.toList());
        return children;
    }
}
